﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SensorNetworkUI;
using System.Data.SqlClient;

namespace WebAccess
{
    public partial class Chart : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Button1.Click+=new EventHandler(Button1_Click);
            if (Session["Username"] == null)
                Response.Redirect("~/login.aspx");
            UserDataStructure refe = (UserDataStructure)Session["Username"];
            
            DropDownList1.DataBind();

            if (!IsPostBack)
            {
                FillDropDown();
            }
            

        }
        protected void FillDropDown()
        {

            SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["sensorsConnectionString"].ConnectionString);
            try
            {

                DropDownList1.DataValueField = "sensor_id";
                DropDownList1.DataTextField = "sensor_name";
                conn.Open();
                SqlCommand comm = conn.CreateCommand();
                comm.CommandText = "select * from sensor";
                SqlDataReader dr = comm.ExecuteReader();
                
                DropDownList1.DataSource = dr;
                DropDownList1.DataBind();
            }
            finally
            {
                conn.Close();
            }


        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            string sens = DropDownList1.Text;
            SqlDataSource1.SelectCommand = "select * from reading where sensor_id = " + sens;
            Chart1.DataBind();
            ChartSpace.Visible = true;
            
           
        }

        protected void SqlDataSource2_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
        {

        }
    }
}